package com.hbter630.change.controller.api;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hbter630.change.DTO.CartproductDTO;
import com.hbter630.change.entity.Cartproduct;
import com.hbter630.change.entity.Order_;
import com.hbter630.change.service.CartproductService;
import com.hbter630.change.service.OrderService;
import com.hbter630.change.service.ProductService;
import com.hbter630.change.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Title: UserCartProductContoller
 * @Author 笑霸fianl
 * @Package com.hbter630.change.controller.api
 * @Date 2023/5/30 15:16
 * @描述: 用户购物车
 */
@RestController
@RequestMapping("/api/UserCartProduct")
@Api(tags = "用户购物车接口")
@Slf4j
public class UserCartProductController {

    @Autowired
    private CartproductService cartproductService;//购物车

    @Autowired
    private ProductService productService;//商品

    @Resource
    private OrderService orderService;


    /**
     * 购物车结算页
     */

    @ApiOperation(notes = "查询所有结算物品",value = "查询所有结算物品",
            httpMethod = "GET", response = Result.class)
    @GetMapping("/list")
    public Result getList(){


        return cartproductService.getListById();
    }

    /**
     * deleteCartById 删除购物车里面的物品
     */
    @ApiOperation(notes = "删除购物车里面的物品",value = "删除购物车里面的物品",
            httpMethod = "GET", response = Result.class)
    @DeleteMapping("/deleteCartById/{id}")
    public Result deleteCartById(@PathVariable("id") Integer id){
        cartproductService.removeById(id);
        return Result.ok().addData(Result.ok());
    }

    /**
     * 将购物车列表 加入订单  ArrayList<CartproductDTO>
     */
    @ApiOperation(notes = "将购物车列表 加入订单",value = "将购物车列表 加入订单",
            httpMethod = "POST", response = Result.class)

    @PostMapping("addOrderTable")
    @Transactional//事务注解
    public Result addOrderTable(@RequestBody List<CartproductDTO> str){
        log.info("\n addOrderTable的controller进来了:{}",str);

        //List<CartproductDTO> cartproductDTOS1 = JSONUtil.toList(str, CartproductDTO.class);

        return cartproductService.addOrderTable(str);

    }

    /**
     * 查看当前用户的订单表
     *
     */
    @ApiOperation(notes = "查看当前用户的订单表 ",value = "查看当前用户的订单表",
            httpMethod = "GET", response = Result.class)

    @GetMapping("finalOrderTable")
    public Result finalOrderTable(){
        int loginIdAsInt = StpUtil.getLoginIdAsInt();//当前用户id
        QueryWrapper<Order_> order_queryWrapper = new QueryWrapper<>();
        order_queryWrapper.eq("user_id",loginIdAsInt);
        List<Order_> list = orderService.list(order_queryWrapper);
        return Result.ok().addData(list);
    }


}
